let arr = [
  [1,2,9],
  [2,3,4,5,4,3,2],
  [3,4,5,[6,7,8,[9]]]
]
// 1. 自定义函数
function customFlatten(arr) {
  let flat = [];
  for(let i of arr) {
    if (Array.isArray(i)) {
      flat = flat.concat(customFlatten(i));
    } else  {
      flat.push(i)
    }
  }
  return flat
}
let arr1 = customFlatten(arr);
// console.log('arr1', arr1)

// 2. 使用 reduce
let flatReduce = (arr) => {
  return arr.reduce((pre, cur) => {
    return pre.concat( Array.isArray(cur)? flatReduce(cur): cur)
  }, [])
}
const arr2 = flatReduce(arr)
// console.log('arr2', arr2)


// 3. 使用flat()
let arr3 = arr.flat(Infinity)
// console.log('arr3', arr3)


// 4. 正则
let arr4 =  JSON.parse('[' + JSON.stringify(arr).replace(/\[|\]/g, '') + ']')
// console.log('arr4', arr4)



/**
 * 进阶 -  去重&排序
 * */ 

const arr5 = [...new Set(arr4)]
console.log('arr5', arr5.sort((a, b) => a-b))
console.log(arr)
